//
//  AutoHomeTbaBar.m
//  AutoHome
//
//  Created by wqh on 15-5-9.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "AutoHomeTbaBar.h"
#import "BaseViewController.h"

@interface AutoHomeTbaBar ()
{
    NSUInteger _menuIndex;
}
@end

@implementation AutoHomeTbaBar

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createMyTabBar];
    [self createViewControllers];
    self.tabBar.backgroundColor = [UIColor yellowColor];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.selectedIndex = _menuIndex;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    _menuIndex = self.selectedIndex;
}

//定制tabBar
- (void)createMyTabBar {
    //1.隐藏系统的
    self.tabBar.hidden = YES;
    //2.定义自己的 tabBar的大小 320*49
    self.customTabBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, kScreenHeight-49, kScreenWidth, 49)];
    self.customTabBar.image = [UIImage imageNamed:@"tagBar_bg_click"];
    self.customTabBar.tag = 100;
    
    //把button 和label 粘贴到imageView
    self.customTabBar.userInteractionEnabled = YES;//打开用户交互
    
    //    NSArray *titles = @[@"first",@"second",@"third",@"fourth"];
    //tabBar的标签大小 30*30
    for (int i = 0;i < 5;i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat space = 0;
        CGFloat padding = (kScreenWidth - 2*space-5*64)/4;
        button.frame = CGRectMake(space+(64+padding)*i, 0, 64, 44);
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"item0%d",i+1]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"item0%d_selected",i+1]] forState:UIControlStateSelected];
        if (i == 0) {
            //默认选中第0个
            self.selectedIndex = 0;
            button.selected = YES;
        }
        button.tag = 101+i;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.customTabBar addSubview:button];
    }
    
    [self.view addSubview:self.customTabBar];
}


- (void)btnClick:(UIButton *)button {
    NSInteger index = button.tag-101;
    //设置选中的索引 那么 就会切换视图
    self.selectedIndex = index;
    //选中
    button.selected = YES;
    
    //遍历 imageView 的所有子视图 找出UIButton
    for (UIView *view in button.superview.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)view;
            if (btn.tag != button.tag) {
                btn.selected = NO;//把其他非选中的按钮 置为NO
            }
        }
    }
}

- (void)createViewControllers {
    
    NSMutableArray *array = [NSMutableArray array];
    NSArray *controllersArr = @[@"RecommendViewController",@"ForumViewController",@"CarViewController",@"DiscoverViewController",@"UserViewController"];
    
    for (NSInteger index = 0; index < controllersArr.count; index++) {
        Class className = NSClassFromString(controllersArr[index]);
        BaseViewController *vc = [[className alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        
        
        [array addObject:nav];
    }
    self.viewControllers = array;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
